package com.ztc.controller;
import com.ztc.beans.Book;
import com.ztc.beans.Jbook;
import com.ztc.beans.User;
import com.ztc.service.BookDao;
import com.ztc.service.BookService;
import com.ztc.service.UserService;
import com.ztc.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
@Controller
@RequestMapping("/Book")
public class BookController extends HttpServlet {
    @Autowired
    UserService userService;
    @Autowired
    BookService bookService;
    /*Scanner sc = new Scanner(System.in);
    BookDao bookDao = new BookDao();
*/
   /* public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);

    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      request.setCharacterEncoding("utf-8");
        String method =  request.getParameter("method");
        if (method.equals("addbook")){
            addbook(request,response);
        }else if (method.equals("upbook")){
            upbook(request,response);
        }else if (method.equals("delbook")){
            delbook(request,response);
        }else if (method.equals("updatebook")){
            updatebook(request,response);
        }else if(method.equals("addjbook")){
            addjbook(request,response);
        }else if(method.equals("viewjbook")){
            viewjbook(request,response);
        }else if(method.equals("getuserlist")){
            getuserlist(request,response);
        }else if(method.equals("upjlist")){
            upjlist(request,response);
        }else if(method.equals("inquire")){
            inquire(request);
        }
    }*/
    @RequestMapping("/inquire")
    private  String inquire(HttpServletRequest request) {

        User user = (User)request.getSession().getAttribute("user");
        request.setAttribute("name", user.getName());
        Page page = new Page<>();
        //获取当前页
        String pagesize =request.getParameter("pagesize")==null?"1":request.getParameter("pagesize");
        page.setCurrentPage(Integer.parseInt(pagesize));
        String bname=request.getParameter("bname")==null?"":request.getParameter("bname");
        String author=request.getParameter("author")==null?"":request.getParameter("author");
        List<Book> blist = bookService.getbooks(bname,author,page);
        int sum = bookService.getBookSum(bname,author);
        page.setRecordTotal(sum);
        page.setContent(blist);
        if(blist.size()>0){
            request.setAttribute("page",page);
            request.setAttribute("bname",bname);
            request.setAttribute("author",author);
        }else{
           /* blist = bookService.getbooks(page);
            page.setContent(blist);
            page.setRecordTotal(0);*/
            request.setAttribute("msg","查询的条件没有参数");
            /*request.setAttribute("list",blist);*/
        }
       /* try {
            request.getRequestDispatcher("/adminlist.jsp").forward(request,response);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }*/
        return "adminlist";

    }

    private void upjlist(HttpServletRequest request, HttpServletResponse response) {
       String bid =request.getParameter("bid");
       String id = request.getParameter("id");

       boolean b = bookService.getjbook(id);
       if(b){
           bookService.upjbook(id);
           bookService.upbookBsum2(bid);
           request.setAttribute("msg","还书成功!");

       }else{
           request.setAttribute("msg","不需要还！");
        }
        getuserlist(request,response);
    }

    private void viewjbook(HttpServletRequest request, HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");
        List<Jbook> jlist= bookService.getjbooks(user.getId());
        request.setAttribute("list",jlist);
        try {
            try {
                request.getRequestDispatcher("/userjbook.jsp").forward(request,response);
            } catch (ServletException e) {
                throw new RuntimeException(e);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //借书
    private void addjbook(HttpServletRequest request, HttpServletResponse response) {
        String bid =request.getParameter("bid");
        User user = (User)request.getSession().getAttribute("user");
        boolean b = bookService.getjbook(user.getId(),bid);

        if(b){
            request.setAttribute("msg","请归还图书后，再次借阅");
        }else{
            bookService.addjbook(user.getId(),bid);
            bookService.upbookBsum1(Integer.parseInt(bid));
            request.setAttribute("msg","借阅成功!");
        }
        getuserlist(request,response);
    }

    @RequestMapping("/getuserlist")
    private String getuserlist(HttpServletRequest request, HttpServletResponse response) {
        List<Book> blist = userService.getbook();
        request.setAttribute("list",blist);
        return "userlist";
      /*  try {
            request.getRequestDispatcher("/userlist.jsp").forward(request,response);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }*/
    }
    @RequestMapping("/upbook")
    private String upbook(@RequestParam ("id") String id, HttpServletRequest request) {
        Integer id1 = Integer.parseInt(id);
        Book book = bookService.getBookById1(id1);
        request.setAttribute("Book",book);
        /*try {
            request.getRequestDispatcher("/upbook.jsp").forward(request,response);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }*/
        return "upbook";
    }



    // 更新列表
    private void getbook(HttpServletRequest request, HttpServletResponse response) {
        List<Book> blist = userService.getbook();
        request.setAttribute("list",blist);
        request.setAttribute("name","张三");
        try {
            request.getRequestDispatcher("/adminlist.jsp").forward(request,response);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //加入书籍
    @RequestMapping("/addbook")
    private String addbook(HttpServletRequest request) {
        Book book = new Book();
        book.setName(request.getParameter("bookname"));
        boolean b = bookService.getBookName(book.getName());
        if(b){
            request.setAttribute("msg","该图书已存在");

        }else {
            book.setAuthor(request.getParameter("author"));
            book.setBsum(Integer.parseInt(request.getParameter("bsum")));
            book.setPrice(Double.parseDouble(request.getParameter("price")));
            bookService.addBoook(book);
            request.setAttribute("msg","添加成功");

        }
        return "redirect:/Book/inquire";
    }


    //修改图书
    @RequestMapping("/updatebook")
    private String updatebook(HttpServletRequest request) {
        String id = request.getParameter("id");
        Book book = bookService.getBookById(Integer.parseInt(id));
        if(book == null){
            request.setAttribute("msg","该图书不存在");
        }else{
            Book book1 = new Book();
            book1.setId(Integer.parseInt(id));
            Book book2 = bookService.getBookById(request.getParameter("bookname"));
            if(book2!=null&&!book.getName().equals(request.getParameter("bookname"))){
                request.setAttribute("msg","该书名已存在");
            }else{
                book1.setId(Integer.parseInt(id));
                book1.setName(request.getParameter("bookname"));
                book1.setAuthor(request.getParameter("author"));
                book1.setBsum(Integer.parseInt(request.getParameter("bsum")));
                book1.setPrice(Double.parseDouble(request.getParameter("price")));
                bookService.upBook(Integer.parseInt(id),book1);
                request.setAttribute("msg","修改成功");
            }
            return "redirect:/Book/inquire";
        }
        return "redirect:/Book/inquire";
    }
    //删除图书
    @RequestMapping("/delbook")
    private String delbook(HttpServletRequest request,@RequestParam("id") String id) {
        int id1 =Integer.parseInt(id);
        Book book = bookService.getBookById(id1);
        if(book == null){
            System.out.println("要删除的图书不存在");
            request.setAttribute("msg","该图书名不存在");
        }else{
            bookService.delBook(id1);
            System.out.println("删除成功！！！");
        }
        return "redirect:/Book/inquire";

    }





}
